// 模拟RustFS相关模块，用于开发和测试

use std::collections::HashMap;
use std::sync::{Arc, Mutex};

// 模拟对象元数据
pub struct ObjectMetadata {
    pub size: u64,
    pub is_dir: bool,
}

// 模拟对象存储
pub struct MockObjectStore {
    data: Arc<Mutex<HashMap<String, (Vec<u8>, ObjectMetadata)>>>,
    bucket_name: String,
}

impl MockObjectStore {
    pub fn new(bucket_name: &str) -> Self {
        MockObjectStore {
            data: Arc::new(Mutex::new(HashMap::new())),
            bucket_name: bucket_name.to_string(),
        }
    }

    pub fn get_object(&self, bucket: &str, key: &str, _options: Option<&()>) -> Result<Vec<u8>, String> {
        if bucket != self.bucket_name {
            return Err(format!("Bucket not found: {}", bucket));
        }

        let data = self.data.lock().unwrap();
        if let Some((content, _)) = data.get(key) {
            Ok(content.clone())
        } else {
            Err(format!("Object not found: {}", key))
        }
    }

    pub fn put_object(&self, bucket: &str, key: &str, content: Vec<u8>, _options: &()) -> Result<(), String> {
        if bucket != self.bucket_name {
            return Err(format!("Bucket not found: {}", bucket));
        }

        let is_dir = key.ends_with('/') && content.is_empty();
        let mut data = self.data.lock().unwrap();
        data.insert(key.to_string(), (
            content,
            ObjectMetadata {
                size: content.len() as u64,
                is_dir,
            }
        ));
        Ok(())
    }

    pub fn delete_object(&self, bucket: &str, key: &str) -> Result<(), String> {
        if bucket != self.bucket_name {
            return Err(format!("Bucket not found: {}", bucket));
        }

        let mut data = self.data.lock().unwrap();
        if data.remove(key).is_some() {
            Ok(())
        } else {
            Err(format!("Object not found: {}", key))
        }
    }

    pub fn list_objects(&self, bucket: &str, prefix: Option<&str>, _marker: Option<&str>, _max_keys: u64) -> Result<Vec<String>, String> {
        if bucket != self.bucket_name {
            return Err(format!("Bucket not found: {}", bucket));
        }

        let data = self.data.lock().unwrap();
        let mut results = Vec::new();

        for key in data.keys() {
            if let Some(prefix_str) = prefix {
                if key.starts_with(prefix_str) {
                    results.push(key.to_string());
                }
            } else {
                results.push(key.to_string());
            }
        }

        Ok(results)
    }

    pub fn stat_object(&self, bucket: &str, key: &str) -> Result<ObjectMetadata, String> {
        if bucket != self.bucket_name {
            return Err(format!("Bucket not found: {}", bucket));
        }

        let data = self.data.lock().unwrap();
        if let Some((_, metadata)) = data.get(key) {
            Ok(metadata.clone())
        } else {
            Err(format!("Object not found: {}", key))
        }
    }
}

// 模拟对象选项
pub struct ObjectOptions {} 

impl Default for ObjectOptions {
    fn default() -> Self {
        ObjectOptions {}
    }
}

// 模拟全局存储实例
lazy_static::lazy_static! {
    static ref GLOBAL_STORE: Arc<Mutex<Option<MockObjectStore>>> = Arc::new(Mutex::new(None));
}

// 初始化全局存储
pub fn init_global_store(bucket_name: &str) {
    let mut store = GLOBAL_STORE.lock().unwrap();
    *store = Some(MockObjectStore::new(bucket_name));
}

// 获取对象层
pub fn new_object_layer_fn() -> Option<MockObjectStore> {
    let store = GLOBAL_STORE.lock().unwrap();
    store.clone()
}